<script setup>
import { ref } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import { userRegisterService, userLoginService } from '@/api/user.js'
import { useUserStore } from '@/stores'
import { useRouter } from 'vue-router'

const loginForm = ref({
  username: '',
  password: ''
})
const registerForm = ref({
  username: '',
  password: '',
  repassword: ''
})

const loginRules = {
  username: [
    {
      required: true,
      message: '请输入用户名',
      trigger: 'blur'
    }
  ],
  password: [
    {
      required: true,
      message: '请输入密码',
      trigger: 'blur'
    }
  ]
}

const registerRules = {
  username: [
    {
      required: true,
      message: '请输入用户名',
      trigger: 'blur'
    }
  ],
  password: [
    {
      required: true,
      message: '请输入密码',
      trigger: 'blur'
    }
  ],
  repassword: [
    {
      required: true,
      message: '请输入密码',
      trigger: 'blur'
    },
    {
      validator: (rule, value, callback) => {
        if (value !== registerForm.value.password) {
          callback(new Error('两次输入密码不一致'))
        } else {
          callback()
        }
      }
    }
  ]
}
const isLogin = ref(true)
const form = ref()
const registerBtn = async () => {
  await form.value.validate()
  await userRegisterService(registerForm.value)
  ElMessage.success('注册成功')
  isLogin.value = !isLogin.value
}

const userStore = useUserStore()
const router = useRouter()
const loginBtn = async () => {
  await form.value.validate()
  const res = await userLoginService(loginForm.value)
  userStore.setToken(res.data.token)
  ElMessage.success('登录成功')
  router.push('/')
}
</script>

<template>
  <div class="form-container">
    <!-- 登录 -->
    <div class="form-card" v-if="isLogin">
      <div class="form-header">
        <h2>欢迎登录</h2>
      </div>
      <el-form :model="loginForm" :rules="loginRules" ref="form" class="login-form">
        <el-form-item prop="username">
          <el-input
            :prefix-icon="User"
            v-model="loginForm.username"
            placeholder="请输入用户名"
            class="custom-input"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            :prefix-icon="Lock"
            v-model="loginForm.password"
            type="password"
            show-password
            placeholder="请输入密码"
            class="custom-input"
          />
        </el-form-item>

        <div class="form-options">
          <!-- <el-checkbox v-model="form.remember">记住我</el-checkbox> -->
          <a @click="isLogin = !isLogin" class="registerOrLogin-Label">注册➡</a>
          <a href="#" class="forgot-link">忘记密码?</a>
        </div>

        <el-form-item>
          <el-button @click="loginBtn" type="primary" class="login-button">登录</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 注册 -->
    <div class="form-card" v-else>
      <div class="form-header">
        <h2>注册</h2>
      </div>
      <el-form :model="registerForm" :rules="registerRules" ref="form" class="login-form">
        <el-form-item prop="username">
          <el-input
            :prefix-icon="User"
            v-model="registerForm.username"
            placeholder="请输入用户名"
            class="custom-input"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            :prefix-icon="Lock"
            v-model="registerForm.password"
            type="password"
            show-password
            placeholder="请输入密码"
            class="custom-input"
          />
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            :prefix-icon="Lock"
            v-model="registerForm.repassword"
            type="password"
            show-password
            placeholder="再次输入密码"
            class="custom-input"
          />
        </el-form-item>

        <el-form-item>
          <el-button @click="registerBtn" type="primary" class="login-button">注册</el-button>
        </el-form-item>

        <div class="form-options">
          <!-- <el-checkbox v-model="form.remember">记住我</el-checkbox> -->
          <a @click="isLogin = !isLogin" class="registerOrLogin-Label">⬅登录</a>
        </div>
      </el-form>
    </div>
  </div>
</template>

<style scoped>
.form-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  width: 100%;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.form-card {
  width: 380px;
  padding: 40px;
  border-radius: 16px;
  background-color: white;
  box-shadow: 0 8px 24px rgba(149, 157, 165, 0.2);
  transition: all 0.3s ease;
}

.form-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 12px 28px rgba(149, 157, 165, 0.3);
}

.form-header {
  text-align: center;
  margin-bottom: 32px;
}

.form-header h2 {
  color: #334155;
  margin-bottom: 8px;
  font-weight: 600;
}

.login-form :deep(.el-form-item) {
  margin-bottom: 24px;
}

.custom-input :deep(.el-input__wrapper) {
  border-radius: 8px;
  padding: 12px 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
}

.custom-input :deep(.el-input__wrapper:hover) {
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.custom-input :deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px #4b70e2 inset !important;
}

.custom-input :deep(.el-input__inner) {
  height: 20px;
}

.custom-input :deep(.el-input__prefix) {
  color: #64748b;
}

.login-button {
  width: 100%;
  padding: 12px 0;
  border-radius: 8px;
  background: linear-gradient(135deg, #4b70e2 0%, #6386f8 100%);
  border: none;
  font-weight: 500;
  transition: all 0.3s ease;
}

.login-button:hover {
  background: linear-gradient(135deg, #3b5fce 0%, #5074e6 100%);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(75, 112, 226, 0.3);
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  font-size: 14px;
}

.forgot-link {
  color: #4b70e2;
  text-decoration: none;
  transition: all 0.2s ease;
}

.forgot-link:hover {
  color: #3b5fce;
  text-decoration: underline;
}

.registerOrLogin-Label {
  color: #000000;
  transition: all 0.2s ease;
}

.registerOrLogin-Label:hover {
  color: #3b5fce;
  transform: translateY(-2px);
}
</style>
